package com.acme.entity;

import com.acme.entity.util.ChangeSupportList;
import com.acme.entity.util.EntityBean;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.swing.event.ChangeListener;

/**
 *
 * @author alejandro
 */
@Entity
@Table(name = "TIPOCASETA", catalog = "carreteras", schema = "dbo")
@NamedQueries({
    @NamedQuery(name = "TipoCaseta.findAll", query = "SELECT t FROM TipoCaseta t"),
    @NamedQuery(name = "TipoCaseta.findById", query = "SELECT t FROM TipoCaseta t WHERE t.id = :id"),
    @NamedQuery(name = "TipoCaseta.findByDescripcion", query = "SELECT t FROM TipoCaseta t WHERE t.descripcion = :descripcion")})
public class TipoCaseta extends EntityBean<TipoCaseta> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "descripcion", nullable = false, length = 30)
    private String descripcion;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tipoCaseta")
    private final ChangeSupportList<Caseta> casetaList = new ChangeSupportList<Caseta>();

    public TipoCaseta() {
    }

    public TipoCaseta(Integer id) {
        this.id = id;
    }

    public TipoCaseta(Integer id, String descripcion) {
        this.id = id;
        this.descripcion = descripcion;
    }

    public TipoCaseta(Integer id, String descripcion, List<Caseta> casetaList) {
        this.id = id;
        this.descripcion = descripcion;
        mergeListContent(this.casetaList, casetaList);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        support.fireChange();
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
        support.fireChange();
    }

    public List<Caseta> getCasetaList() {
        return casetaList;
    }

    public void setCasetaList(List<Caseta> casetaList) {
        mergeListContent(this.casetaList, casetaList);
//        support.fireChange(); <-- La lista ya controla los cambios
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof TipoCaseta)) {
            return false;
        }
        TipoCaseta other = (TipoCaseta) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return descripcion;
    }

    @Override
    public void addChangeListener(ChangeListener listener) {
        casetaList.addChangeListener(listener);
        super.addChangeListener(listener);
    }

    @Override
    public TipoCaseta clone() {
        return new TipoCaseta(id, descripcion, casetaList);
    }

    @Override
    public void merge(TipoCaseta other) {
        this.id = other.id;
        this.descripcion = other.descripcion;
        mergeListContent(this.casetaList, other.casetaList);
    }

}
